package com.wjbgn.electronicnian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.electronicnian.convert.NianSayLogDoConvert;
import com.wjbgn.electronicnian.dto.NianSayLogDTO;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianSayLogDO;
import com.wjbgn.electronicnian.service.NianSayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 年兽说的话
 * Create Date: 2022-01-22T15:50:35.292688500
 *
 * @author weirx
 * @version 1.0
 */
@Api(tags = "年兽说的话")
@RestController
@RequestMapping("/niansaylog")
@AllArgsConstructor
public class NianSayLogController {

    /**
     * NianSayLogService
     */
    private NianSayLogService nianSayLogService;

    /**
     * 分页列表
     *
     * @param nianSayLogDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(NianSayLogDTO nianSayLogDTO) {
        QueryWrapper<NianSayLogDO> queryWrapper = new QueryWrapper<>(NianSayLogDoConvert.dtoToDo(nianSayLogDTO));
        Page<NianSayLogDO> page = new Page<>(nianSayLogDTO.getPage(), nianSayLogDTO.getLimit());
        Page<NianSayLogDO> pageList = nianSayLogService.page(page, queryWrapper);
        return Result.success(NianSayLogDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     *
     * @param nianId
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(@RequestParam("nianId") long nianId) {
        List<NianSayLogDO> list = nianSayLogService.lambdaQuery()
                .eq(NianSayLogDO::getNianId, nianId)
                .orderByDesc(NianSayLogDO::getCreateTime)
                .last("limit 15")
                .list();
        return Result.success(NianSayLogDoConvert.listConvert(list));
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public Result info(@RequestParam("id") Long id) {
        NianSayLogDO nianSayLog = nianSayLogService.getById(id);
        return Result.success(null == nianSayLog ? null : NianSayLogDoConvert.doToDto(nianSayLog));
    }

    /**
     * 新增
     *
     * @param nianSayLogDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody NianSayLogDTO nianSayLogDTO) {
        boolean flag = nianSayLogService.save(NianSayLogDoConvert.dtoToDo(nianSayLogDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 更新
     *
     * @param nianSayLogDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody NianSayLogDTO nianSayLogDTO) {
        boolean flag = nianSayLogService.updateById(NianSayLogDoConvert.dtoToDo(nianSayLogDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }
}